/**
主要回顾：HTTP 协议
HTTP版本：
	HTTP1.0：一次连接一次请求
	HTTP1.1：一次连接多次请求

HTTP 请求：
	GET /day09/HelloServlet HTTP/1.1
	Host: localhost:8080
	Connection: keep-alive
	Upgrade-Insecure-Requests: 1
	User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36
	Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,* /*;q=0.8
	Accept-Encoding: gzip, deflate, sdch, br
	Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4

	常见的请求方式： GET 、 POST、 HEAD、 TRACE、 PUT、 CONNECT 、DELETE	
	常用的请求方式： GET  和 POST
	
	GET：数据跟在 URI 后
	POST：数据在请求实体中

请求头：
	Accept: text/html,image/*		-- 浏览器接受的数据类型
	Accept-Charset: ISO-8859-1		-- 浏览器接受的编码格式
	Accept-Encoding: gzip,compress	--浏览器接受的数据压缩格式
	Accept-Language: en-us,zh-		--浏览器接受的语言
	Host: www.iamteer.com:80		--（必须的）当前请求访问的目标地址（主机:端口）
	If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT	--浏览器最后的缓存时间
	Referer: http://blog.iamteer.com/	--当前请求来自于哪里
	User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)  --浏览器类型
	Cookie:name=chuck				-- 浏览器保存的cookie信息
	Connection: close/Keep-Alive	-- 浏览器跟服务器连接状态。close: 连接关闭  keep-alive：保存连接。
	Date: Tue, 11 Jul 2000 18:23:51 GMT	 -- 请求发出的时间
 */

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RequestInfo
 */
@WebServlet("/RequestInfo")
public class RequestInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
		//请求行
		System.out.println("请求方式：" + request.getMethod());
		System.out.println("URI：" + request.getRequestURI());
		System.out.println("URL：" + request.getRequestURL());
		System.out.println("HTTP 协议版本：" + request.getProtocol());
		//请求头
		String host = request.getHeader("Host");
		System.out.println(host);
		
		Enumeration<String> headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String headerName = (String) headerNames.nextElement();
			String headerValue = request.getHeader(headerName);
			System.out.println(headerName + ":" + headerValue);
		}
		//请求实体内容——GET 方式中的实体内容为空
		ServletInputStream in = request.getInputStream();
		byte[] buf = new byte[1024];
		int len = -1;
		System.out.println("请求实体内容:");
		while ((len = in.read(buf)) != -1) {
			System.out.println(new String(buf, 0, len));
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//请求实体内容
		ServletInputStream in = req.getInputStream();
		byte[] buf = new byte[1024];
		int len = -1;
		System.out.println("请求实体内容:");
		while ((len = in.read(buf)) != -1) {
			System.out.println(new String(buf, 0, len));
		}
	}
}
